package com.hsxc.cdz.trans.bill;

import com.hsxc.cdz.base.bean.Bill;
import com.hsxc.cdz.base.dao.BillDao;
import com.hsxc.cdz.base.util.CommonUtils;
import com.hsxc.cdz.base.util.TimeFormatter;
import com.hsxc.cdz.mgmt.GlobalManager;
import com.hsxc.cdz.monitor.FSM.CachePile;
import com.hsxc.cdz.trans.bill.CacheTrade.TradeStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

@Service
public class BillManager {

    private Logger logger = LoggerFactory.getLogger(BillManager.class);
    @Autowired
    private BillDao billDao;

    public BillManager() {
        super();
    }

    /**
     * 充电确认时，创建账单 如果不存在交易则为刷卡账单 如果不为空，为app或者公众号账单
     *
     * @param bill
     * @return
     */
    public int createBill(Bill bill) {
//		CacheTrade cacheTrade = GlobalManager.tradeManager.getTrade(bill.getPileNO(), bill.getUserNO());
//		if (cacheTrade == null) {
//			bill.setBillNO(CommonUtils.getTID());
//		}
//		bill.setBillNO(cacheTrade.getTid());
        return billDao.insertBill(bill);
    }

    /**
     * 通过账单的单号修改账单的充电电量及总费用
     *
     * @param billNo
     * @return
     */
    public int updateBill(String billNo, double electricity, double feeSum) {
        Bill bill = new Bill();
        bill.setBillNO(billNo);
        bill.setChargeFee(feeSum);
        bill.setChargeDegree(electricity);
        billDao.updateBill(bill);
        return 1;
    }

    public Bill getBillByBillNO(String billNO) {
        Bill bill = new Bill();
        bill.setBillNO(billNO);
        return billDao.selectBill(bill);
    }

    public List<Bill> getAllBills() {
        Bill bill = new Bill();
        return billDao.selectBills(bill);
    }

    public List<Bill> getBillsByPileNO(String pileNO) {
        Bill bill = new Bill();
        bill.setPileNO(pileNO);
        return billDao.selectBills(bill);
    }

    // 根据充电桩编号串查找账单
    public List<Bill> getBillsByPileNOs(Set<String> pileNOs) {

        List<Bill> bills = new ArrayList<>();
        List<Bill> result = new ArrayList<>();
        for (Iterator<String> iterator = pileNOs.iterator(); iterator.hasNext(); ) {
            String pileNO = iterator.next();
            bills = getBillsByPileNO(pileNO);
            result.addAll(bills);
        }
        logger.info("size-------"+result.size());
        result.forEach(x->logger.info(x.getBillNO()));
        return result;
    }

    public List<Bill> getBillByUserNO(String userNO) {
        Bill bill = new Bill();
        bill.setUserNO(userNO);
        return billDao.selectBills(bill);
    }

    public int updateBillByHeart(String pileNO, double chargeDegree, double chargeFee) {

        CacheTrade cacheTrade = GlobalManager.tradeManager.getTradeByPileAddress(pileNO);
        if (cacheTrade == null) {
            return 0;
        } else {
            if (cacheTrade.getTradeStatus() == TradeStatus.TRADE_STATUS_ON) {
                CachePile cachePile = cacheTrade.getCachePile();
                if (cachePile == null) {
                    return 0;
                }

                Bill bill = getBillByBillNO(cacheTrade.getTid());
                bill.setChargeDegree(chargeDegree);
                bill.setChargeFee(chargeFee);
                bill.setStopTime(TimeFormatter.toTimeYear());
                billDao.updateBill(bill);
            }
            return 1;
        }
    }

    public int updateBill(Bill bill) {

        return billDao.updateBill(bill);

    }

    /**
     * 充电完成时，更新账单信息
     *
     * @param bill
     * @return
     */
    public int storeBill(Bill bill) {
        CacheTrade cacheTrade = GlobalManager.tradeManager.getTrade(bill.getPileNO(), bill.getUserNO());
        // BillManager billManager = (BillManager)
        // ApplicationUtil.getBean("billManager");
        if (cacheTrade == null) {// 如果内存中不存在，则是刷卡账单
            bill.setBillNO(CommonUtils.getTID());
            bill.setPayWay(Bill.CARD);
            bill.setState(Bill.PAYED);
            GlobalManager.billManager.createBill(bill);
        } else {
            if (cacheTrade.getOpenId() != null) {
                bill.setPayWay(Bill.WXGZH);
            } else {
                bill.setPayWay(Bill.WXAPP);
            }
            bill.setBillNO(cacheTrade.getTid());
            bill.setState(Bill.NO_PAY);
            if (GlobalManager.billManager.updateBill(bill) == 1) {
                if (bill.getState() != Bill.PAYED) {
                    cacheTrade.setTradeStatus(TradeStatus.TRADE_STATUS_NOPAY);
                    GlobalManager.tradeManager.modifyTrade(cacheTrade.getTid(), cacheTrade);
                } else {
                    GlobalManager.tradeManager.delTrade(cacheTrade.getTid());
                }
            }
        }
        return 0;
    }
}